<?php

namespace backend\models;

//use common\models\CommentsReply;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Comments;


/**
 * CommentsSearch represents the model behind the search form about `common\models\Comments`.
 */
class CommentsSearch extends Comments
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['comment_id'], 'integer'],
            [['content', 'create_time'], 'required'],
            [['content', 'img', 'create_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Comments::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'comment_id' => $this->comment_id,
            'create_time' => $this->create_time,
        ]);

        $query->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'img', $this->img]);

        return $dataProvider;
    }

    /*
     * 刷新Redis
     * */
    public function updateRedis($comment_id){

        $relationData = CommentsRelation::find()->where(['comment_id' => $comment_id])->asArray()->one();

        // 商品评论 用户评论
        if(!empty($relationData)) {
            $m = new CommentsRelation();
            \Yii::$app->redis_comments->hmset('comment_' . $relationData['comment_id'], $relationData);//评论详情

            if ($relationData['is_show'] == 1 && in_array($relationData['status'], array(0, 1))) {

                // 按是否精华、发表时间排序
                \Yii::$app->redis_comments->zadd(
                    'goods_comment_' . $relationData['goods_id'],
                    $relationData['status'] . $relationData['create_time'],
                    $relationData['comment_id']
                );

                $m->updateGoodAndBad($relationData);

                if(in_array($relationData['level'],array(4,5))){
                    //加入好评表
                    \Yii::$app->redis_comments->zadd(
                        'goods_comment_A_' . $relationData['goods_id'],
                        $relationData['status'].$relationData['create_time'],
                        $relationData['comment_id']
                    );
                }
                else{
                    //移出好评表
                    \Yii::$app->redis_comments->zrem(
                        'goods_comment_A_' . $relationData['goods_id'],
                        $relationData['comment_id']
                    );
                }

                if(in_array($relationData['level'], array(2,3))){
                    //加入中评表
                    \Yii::$app->redis_comments->zadd(
                        'goods_comment_B_' . $relationData['goods_id'],
                        $relationData['status'].$relationData['create_time'],
                        $relationData['comment_id']
                    );
                }
                else{
                    //移出中评表
                    \Yii::$app->redis_comments->zrem(
                        'goods_comment_B_' . $relationData['goods_id'],
                        $relationData['comment_id']
                    );
                }

                if($relationData['level'] == 1){
                    //加入差评表
                    \Yii::$app->redis_comments->zadd(
                        'goods_comment_C_' . $relationData['goods_id'],
                        $relationData['status'].$relationData['create_time'],
                        $relationData['comment_id']
                    );
                }
                else{
                    //移出差评表
                    \Yii::$app->redis_comments->zrem(
                        'goods_comment_C_' . $relationData['goods_id'],
                        $relationData['comment_id']
                    );
                }

            } else {
                //移出商品评论列表
                Yii::$app->redis_comments->zrem('goods_comment_' . $relationData['goods_id'], $relationData['comment_id']);
                //移出好评表
                \Yii::$app->redis_comments->zrem(
                    'goods_comment_A_' . $relationData['goods_id'],
                    $relationData['comment_id']
                );
                //移出中评表
                \Yii::$app->redis_comments->zrem(
                    'goods_comment_B_' . $relationData['goods_id'],
                    $relationData['comment_id']
                );
                //移出差评表
                \Yii::$app->redis_comments->zrem(
                    'goods_comment_C_' . $relationData['goods_id'],
                    $relationData['comment_id']
                );
            }

            if ($relationData['status'] == 1) {//精华评论
                // 个人中心 精华评论列表 按发表时间排序
                \Yii::$app->redis_comments->zadd(
                    'user_comment_perfect_' . $relationData['bestry_uid'],
                    $relationData['create_time'],
                    $relationData['comment_id']
                );
            } else {
                Yii::$app->redis_comments->zrem(
                    'user_comment_perfect_' . $relationData['bestry_uid'],
                    $relationData['comment_id']);
            }
            if ($relationData['is_reply'] == 1) {//客服回复列表

                // 个人中心 客服回复列表 按发表时间排序
                \Yii::$app->redis_comments->zadd(
                    'user_comment_reply_' . $relationData['bestry_uid'],
                    $relationData['create_time'],
                    $relationData['comment_id']
                );
            } else {
                Yii::$app->redis_comments->zrem('user_comment_reply_' . $relationData['bestry_uid'],
                    $relationData['comment_id']);
            }

            // 个人中心 用户评论列表
            \Yii::$app->redis_comments->zadd(
                'user_comment_' . $relationData['bestry_uid'],
                $relationData['create_time'],
                $relationData['comment_id']
            );

            $m->updateFeedbackRate($relationData['goods_id']);
            $m->updateUserCommentNumberToRedis($relationData['bestry_uid']);

            $commentsData = $this->find()->where(['comment_id' => $comment_id])->asArray()->one();
            if(!empty($commentsData)) {
                // 评论内容
                \Yii::$app->redis_comments->set('comment_content_' . $commentsData['comment_id'], $commentsData['content']);
                unset($commentsData['content']);

                // 评论其他字段
                \Yii::$app->redis_comments->hmset('comment_' . $commentsData['comment_id'], $commentsData);
            }
            $replyData = CommentsReply::find()->where(['comment_id' => $comment_id])->asArray()->one();
            if(!empty($replyData)) {
                // 回复详情
                Yii::$app->redis_comments->hmset('comment_reply_' . $replyData['comment_id'], $replyData);
            }
        }

    }



}
